/*****************************************************************************
 * kal_linux_mutex.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.mutex"

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    plum_s8          name[PLUM_KAL_MUTEX_NAME_MAX];
    pthread_mutex_t  hdl;
    struct list_head list;
} kal_mutex_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE kal_mutex_t mutex_head;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_s32 kal_mutex_add(kal_mutex_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            LOG_E("kal_mq_add param err");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_add(&node->list, &mutex_head.list);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_mutex_t *kal_mutex_find(PLUM_CONST plum_s8 *name)
{
    struct list_head *pos;
    list_for_each(pos, &mutex_head.list)
    {
        kal_mutex_t *node = list_entry(pos, kal_mutex_t, list);
        if (strcmp((const char *)name, (const char *)node->name) == 0) {
            return (node);
        }
    }

    return (plum_null);
}

PLUM_PRIVATE
plum_s32 kal_mutex_del(kal_mutex_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_del(&node->list);
        plum_kal_mem_free(node);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_mutex_t *kal_mutex_apply(PLUM_CONST plum_s8 *name, pthread_mutex_t hdl)
{
    kal_mutex_t *node = plum_null;
    do {
        if (!mutex_head.list.next) {
            INIT_LIST_HEAD(&mutex_head.list);
        }

        node = kal_mutex_find(name);
        if (!node) {
            node = (kal_mutex_t *)plum_kal_mem_malloc(sizeof(kal_mutex_t));
            if (!node) {
                LOG_E("kal_mutex_apply malloc err");
                break;
            }

            snprintf((char *)node->name, sizeof(node->name), "%s", name);
            node->hdl = hdl;
        }
    } while (0);

    return (node);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_mutex_create(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The mutex name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (strlen((const char *)name) > PLUM_KAL_MUTEX_NAME_MAX) {
            LOG_E("The mutex name is too long");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        pthread_mutexattr_t attr;
        rc = pthread_mutexattr_init(&attr);
        if (rc) {
            LOG_E("pthread_mutexattr_init err,rc:%d", rc);
            break;
        }

        rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
        if (rc) {
            LOG_E("pthread_mutexattr_settype err,rc:%d", rc);
            break;
        }

        pthread_mutex_t mutex;
        rc = pthread_mutex_init(&mutex, &attr);
        if (rc) {
            LOG_E("pthread_mutex_init err,rc:%d", rc);
            break;
        }

        rc = pthread_mutexattr_destroy(&attr);
        if (rc) {
            LOG_E("pthread_mutexattr_destroy err,rc:%d", rc);
            break;
        }

        kal_mutex_t *node = kal_mutex_apply(name, mutex);
        if (!node) {
            LOG_E("kal_mutex_apply err");
            break;
        }

        rc = kal_mutex_add(node);
        if (rc) {
            LOG_E("kal_mutex_add err,rc:%d", rc);
            break;
        }

        LOG_D("create a mutex,named:%s", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_mutex_delete(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The mutex name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_mutex_t *node = kal_mutex_find(name);
        if (!node) {
            LOG_E("not find the mutex,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = pthread_mutex_destroy(&node->hdl);
        if (rc) {
            LOG_E("pthread_mutex_destroy err,rc:%d", rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        rc = kal_mutex_del(node);
        if (rc) {
            LOG_E("kal_mutex_del err,rc:%d", rc);
            break;
        }

        LOG_I("Delete mutex,named:%s", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_mutex_lock(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The mutex name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_mutex_t *node = kal_mutex_find(name);
        if (!node) {
            LOG_E("not find the mutex,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = pthread_mutex_lock(&node->hdl);
        if (rc) {
            LOG_E("pthread_mutex_lock err,rc:%d");
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_mutex_unlock(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            LOG_E("The mutex name is null");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_mutex_t *node = kal_mutex_find(name);
        if (!node) {
            LOG_E("not find the mutex,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = pthread_mutex_unlock(&node->hdl);
        if (rc) {
            LOG_E("pthread_mutex_unlock err,rc:%d");
            rc = PLUM_ECODE_EIO;
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_kal_mutex_show(PLUM_CONST plum_s8 *name)
{
    if (name) {
        kal_mutex_t *node = kal_mutex_find(name);
        if (!node) {
            LOG_E("not find the mutex,named:%d", name);
            return;
        }

        LOG_I("mutex hdl[%p],named:%s", &node->hdl, node->name);
    }
    else {
        struct list_head *pos;
        list_for_each(pos, &mutex_head.list)
        {
            kal_mutex_t *node = list_entry(pos, kal_mutex_t, list);

            LOG_I("mutex hdl[%p],named:%s", &node->hdl, node->name);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
